<?php
/*******************************************************
*
* Page:    app_form.class
* Author:  Matthew Sturges (matt@matthewsturges.com)
* Purpose: Base classes for auto-form generation functionality
* Created: 06/29/05
* Version: 2.0
*
* Modified:
*    01/01/05 AAA  Sample modification notes
*
*******************************************************/

class app_form {

    //Member Variables
    var $name;
    var $action;
    var $method;
    var $on_submit;
    var $upload;

    function app_form($name, $action, $method='POST', $on_submit='', $upload=false){
        $this->name = $name;
        $this->action = $action;
        $this->method = $method;
        $this->on_submit = $on_submit;
        $this->upload = $upload;
    }

    function get_header_html(){

        if ($this->on_submit){
            $on_submit = ' onsubmit="' . $this->on_submit . '"';
        }

        if ($this->upload) {
            $h = "<form name='$this->name' enctype='multipart/form-data' method='$this->method' action='$this->action'$on_submit>\n";
            $h.= "<input type='hidden' name='MAX_FILE_SIZE' value='5000000' />\n";
        } else {
            $h = "<form name='$this->name' action='$this->action' method='$this->method'$on_submit>\n";
        }
        return $h;
    }

    function get_footer_html(){
        $h = "</form>";
        return $h;
    }

} //end class app_form


class app_form_file_upload {
    var $name;

    function app_form_file_upload($name){
        $this->name = $name;
    }

    function get_html() {
	 
        $h = "<input type='file' name='$this->name'>\n";
        return $h;
    } //end function get_html()

}//end class app_form_file_upload

class app_form_hidden {
    var $name;
    var $value;

    function app_form_hidden($name, $value){
        $this->name = $name;
        $this->value = $value;
    }//end function stdFormHidden() (constructor)

    function get_html(){
        $value = htmlspecialchars($this->value);
        $h = "<input type=\"hidden\" name=\"$this->name\" value=\"$value\" />\n";
        return $h;
    }//end function get_html()

}//end class stdFormHidden

class app_form_text {
    //member variables
    var $name;
    var $value;
    var $size;
    var $maxlength;
    var $password;
    var $css_class;

    var $disabled;
    var $id;
    var $on_change;

    function app_form_text($name, $value, $size, $maxlength='', $password=false, $css_class=''){
        $this->name = $name;
        $this->value = $value;
        $this->size = $size;
        $this->maxlength = $maxlength;
        $this->css_class = $css_class;
        $this->password = $password;
    }//end function

    function get_html(){
        $type = ($this->password ? 'password' : 'text');
        if ($this->id) $id = ' id="' . $this->id . '"';
        $value = $this->value;
        $value = str_replace('"', "&quot;", $value);
        $disabled = ($this->disabled ? ' disabled' : '');
        $h = "<input $id onChange=\"$this->on_change\" type=\"$type\" name=\"$this->name\" value=\"$value\" size=\"$this->size\" maxlength=\"$this->maxlength\" class=\"$this->css_class\"$disabled/>";
        return $h;
    }//end function

}// end class

class app_form_textarea {
    //member variables
    var $name;
    var $value;
    var $rows;
    var $cols;
    var $css_class;

    var $disabled;

    function app_form_textarea($name, $value, $rows, $cols, $css_class=''){
        $this->name = $name;
        $this->value = $value;
        $this->rows = $rows;
        $this->cols = $cols;
        $this->css_class = $css_class;
    }//end function

    function get_html(){
        $value = $this->value;
        $value = str_replace("'", "&quot;", $value);
        $disabled = ($this->disabled ? ' disabled' : '');
        $h = "<textarea name='$this->name' rows='$this->rows' cols='$this->cols' class='$this->css_class'$disabled>$this->value</textarea>";
        return $h;
    }//end function

}// end class

class app_form_checkbox {
    //member variables
    var $name;
    var $value;
    var $checked;
    var $label;
    var $on_change;
    var $css_class;

    var $disabled;

    function app_form_checkbox ($name, $value, $checked, $label, $on_change='', $css_class=''){
        $this->name = $name;
        $this->value = $value;
        $this->checked = $checked;
        $this->on_change = $on_change;
        $this->label = $label;
        $this->css_class = $css_class;
    }//end constructor

    function get_html(){
        $name = $this->name;
        $value = $this->value;
        $label = $this->label;
        $disabled = ($this->disabled ? ' disabled' : '');
        if ($this->on_change) {
            $on_change = "onClick='$this->on_change' ";
        } else {
            $on_change = '';
        }
        if ($this->checked) {
            $checked = ' CHECKED';
        } else {
            $checked = '';
        }
        $h = "<input type='checkbox' name='$name' value='$value'$checked class='$this->css_class' $on_change $disabled/>&nbsp;$label";
        return $h;
    }//end function get_html;

} // end class app_form_checkbox


class app_form_select {
    //member variables
    var $name;
    var $value;
    var $option_value_null;
    var $option_text_null;
    var $type;
    var $option_value;
    var $option_text;
    var $query;
    var $option_value_field;
    var $option_text_field;
    var $css_class;
    var $on_change;

    var $disabled;
    var $id;

    function app_form_select($name, $value, $type, $option_value_null='', $option_text_null='', $on_change='', $css_class=''){
        $this->name = $name;
        $this->value = $value;
        $this->type = $type;
        $this->option_value_null = $option_value_null;
        $this->option_text_null = $option_text_null;
        $this->css_class = $css_class;
        $this->on_change = $on_change;
    }


    function get_html(){
        if ($this->css_class) $class = " class='$this->css_class'";
        if ($this->on_change) $change = " onChange='$this->on_change'";
        $disabled = ($this->disabled ? ' disabled' : '');
        if ($this->id) $id = " id='$this->id'";

        $h = "<SELECT name='$this->name'" . $id . $class . $change . $disabled . ">\n";

        //if there's a null value, include it
        if ($this->option_text_null) {
            $h.= "    <OPTION value='" . $this->option_value_null . "'>" . $this->option_text_null . "</option>\n";
        }

        //get options based on type
        if ($this->type == APP_FORM_SELECT_DATABASE) {
            $h.= $this->get_database_options();
				
        } else {
            $h.= $this->get_array_options();
	}

        //end the select
        $h.= "</select>\n";

        //return the value
        return $h;
    }

    function get_database_options(){
        $h = '';

        //get the options from the database
        $result = app_data::mysql_query($this->query);

        //while there are rows in the recordset, draw them out
        while ($line = mysql_fetch_array($result)){
            $val = $line[$this->option_value_field];
            $txt = $line[$this->option_text_field];
				
            //if the value of the option = the value of the field, then mark it selected
            if ($val == $this->value) {
                $selected = ' SELECTED';
            } else {
                $selected = '';
            }
            $h.= "    <option value='$val'$selected>$txt</option>\n";
        }//end while
		  
        return $h;
    }//end function get_database_options()

    function get_array_options(){
        $h = '';

        //If we've only got values, then make the text the same thing, and vice-versa.
        if ($this->option_value && !$this->option_text) $this->option_text = $this->option_value;
        if ($this->option_text && !$this->option_value) $this->option_value = $this->option_text;

        //make sure that these are arrays; if not, make them
        if (!is_array($this->option_value)) $this->option_value = explode(',', $this->option_value);
        if (!is_array($this->option_text)) $this->option_text = explode(',', $this->option_text);

        //error checking
        if (count($this->option_value) != count($this->option_text)) {
            echo "Arrays for option values and text are of different lengths for field $this->name.";
            exit();
        }

        //iterate through the arrays and make the loops
        $ct = count($this->option_value);
        for ($i = 0; $i < $ct; $i++){
            $val = $this->option_value[$i];
            $txt = $this->option_text[$i];

            //if the value of the option = the value of the field, then mark it selected
            if ($val == $this->value) {
                $selected = ' SELECTED';
            } else {
                $selected = '';
            }
            $h.= "    <option value='$val'$selected>$txt</option>\n";
        }//next i

        return $h;
    }//end function;

}//end class app_form_select

class app_form_button {
    var $name;
    var $value;
    var $type;
    var $on_click;
    var $css_class;

    function app_form_button($name, $value, $type='submit', $on_click='', $css_class=''){
        $this->name = $name;
        $this->value = $value;
        $this->type = $type;
        $this->on_click = $on_click;
        $this->css_class = $css_class;
    }

    function get_html(){
        if ($this->on_click) {
            $on_click = " onClick=\"$this->on_click\" ";
        } else {
            $on_click = '';
        }
        $h = "<input type='$this->type' name='$this->name' value='$this->value'$on_click class='$this->css_class' />";
        return $h;
    }//end function
}//end class app_form_button
?>
